//
//  NotifyListViewController.m
//  MerieuxWskProj
//
//  Created by 焱 孙 on 16/1/9.
//  Copyright © 2016年 visionet. All rights reserved.
//

#import "NotifyListViewController.h"
#import "NotifyVo.h"
#import "NotifyListCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "Utils.h"
#import "MJRefresh.h"
#import "ExtScope.h"

@interface NotifyListViewController () <UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *aryNotify;
    NSInteger nPageNum;
}

@property (weak, nonatomic) IBOutlet UITableView *tableViewNotify;

@end

@implementation NotifyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initData];
    [self initView];
}

- (void)initView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setTopNavBarTitle:@"消息"];
    
    if(self.notifyListViewType == NotifyListDefaultType)
    {
        //左边按钮
        UIButton *btnBack = [Utils buttonWithImageName:[UIImage imageNamed:@"nav_setting"] frame:[Utils getNavLeftBtnFrame:CGSizeMake(100,76)] target:self action:@selector(showSideMenu)];
        [self setLeftBarButton:btnBack];
        //notice num view
        NoticeNumView *noticeNumView = [[NoticeNumView alloc]initWithFrame:CGRectMake(25.5, 6+kStatusBarHeight, 18, 18)];
        [self.view addSubview:noticeNumView];
    }
    
    _tableViewNotify.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableViewNotify registerNib:[UINib nibWithNibName:@"NotifyListCell" bundle:nil] forCellReuseIdentifier:@"NotifyListCell"];
    
    //下拉刷新
    @weakify(self)
    _tableViewNotify.mj_header =  [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self loadNewData:YES];
    }];
    
    //上拉加载更多
    _tableViewNotify.mj_footer =  [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self loadNewData:NO];
    }];
}

- (void)initData
{
    nPageNum = 1;
    aryNotify = [NSMutableArray array];
    [self loadNewData:YES];
}

//分页数据
-(void)loadNewData:(BOOL)bRefresh
{
    if (bRefresh)
    {
        //下拉刷新
        nPageNum = 1;
    }
    else
    {
        //上拉加载
    }
    
    [ServerProvider getNotifyList:nPageNum size:15 result:^(ServerReturnInfo *retInfo) {
        if (retInfo.bSuccess)
        {
            NSMutableArray *aryData = retInfo.data;
            if (bRefresh)
            {
                //下拉刷新
                if (aryData != nil && aryData.count>0)
                {
                    [aryNotify removeAllObjects];
                    [aryNotify addObjectsFromArray:aryData];
                }
            }
            else
            {
                //上拉加载
                [aryNotify addObjectsFromArray:aryData];
            }
            
            if (aryData != nil && aryData.count>0)
            {
                nPageNum ++;
            }
            [_tableViewNotify reloadData];
        }
        else
        {
            [Common tipAlert:retInfo.strErrorMsg];
        }
        
        //停止刷新
        if (bRefresh)
        {
            if([retInfo.data2 boolValue])
            {
                [_tableViewNotify.mj_footer endRefreshingWithNoMoreData];   //最后一页
            }
            [_tableViewNotify.mj_header endRefreshing];
        }
        else
        {
            if([retInfo.data2 boolValue])
            {
                [_tableViewNotify.mj_footer endRefreshingWithNoMoreData];   //最后一页
            }
            else
            {
                [_tableViewNotify.mj_footer endRefreshing];
            }
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)showSideMenu
{
    [[NSNotificationCenter defaultCenter]postNotificationName:kDrawerOpenLeftSide object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if(self.notifyListViewType == NotifyListDefaultType)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:kDrawerAddPanGesture object:nil];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    if(self.notifyListViewType == NotifyListDefaultType)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:kDrawerRemovePanGesture object:nil];
    }
}

- (void)configureCell:(NotifyListCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.fd_enforceFrameLayout = NO;
    cell.entity = aryNotify[indexPath.row];
}

#pragma mark Table View
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return aryNotify.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotifyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotifyListCell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //采用自动计算高度，并且带有缓存机制
    return [tableView fd_heightForCellWithIdentifier:@"NotifyListCell" cacheByIndexPath:indexPath configuration:^(NotifyListCell *cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
